package Thread;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 苏李涛
 * Date: 2024-10-16
 * Time: 12:35
 */
public class Demo2 {

    public static void main(String[] args) {
        Object locker = new Object();
        Object locker2 = new Object();

        Thread t1 = new Thread(() -> {
            try {
                System.out.println("wait 之前");
                synchronized (locker) {
                    locker.wait();
                }

                System.out.println("wait 之后");
            } catch (InterruptedException e) {
                throw new RuntimeException();
            }
        });

        Thread t2 = new Thread(() -> {
            Scanner scanner = new Scanner(System.in);
            System.out.println("输入任意内容, 通知唤醒 t1");
            scanner.next();

            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            //注意要先wait，再notify

            synchronized (locker) {
                locker.notify();
            }
        });

        t1.start();
        t2.start();
    }
}
